package com.roc.web.action.setting;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.page.BaseAction;
import com.roc.pojo.Operators;
import com.roc.service.IExcelService;
import com.roc.service.ServiceException;
import com.roc.service.setting.ISettingExcelService;
import com.roc.util.Constance;
import com.roc.util.ExcelUtil;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
import com.roc.web.action.estrn.EstrnHisInfoSearchCondition;
@Component
@Scope("prototype")
@Namespace("/settingExcel")
@SuppressWarnings("all")
public class SettingExcelAction extends BaseAction{
	static Logger  logger=Logger.getLogger(SettingExcelAction.class);
	@Autowired
	ISettingExcelService excelService;
	private  SettingSearchCondition searchCondition;
	
	
	
	private InputStream excelFile;
	private String  excelName;
	//结算导出excel
	@Action(value="settingExcelqQuery",results = {@Result(name="success",type="stream"
			,params = { "contentType", "application/vnd.ms-excel", "inputName",  
			            "excelFile","contentDisposition",  "attachment;filename=${excelName}.xls",
 "bufferSize", "1024*3" }),@Result(name="error",location="/pages/setting/settinglist.jsp") })  
	public String  settingExcelqQuery() {
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		String  mid=null;
		try{
			if(searchCondition.getMinDate()==null || searchCondition.getMinDate().trim().length()==0){
				addActionError("请选择交易起始日期！");
				return ERROR;
			}
			if(searchCondition.getMaxDate()==null || searchCondition.getMaxDate().trim().length()==0){
				addActionError("请选择交易终止日期！");
				return ERROR;
			}
			
			boolean isTrue=Constance.theDateIsTrue(searchCondition.getMinDate(),searchCondition.getMaxDate());
			if(!isTrue){
				addActionError("您选择的交易日期必须在一个月之内！");
				return ERROR;
			}
			HSSFWorkbook work=excelService.settingQueryExcel(searchCondition);
			 ByteArrayOutputStream output = new ByteArrayOutputStream();  
			         work.write(output);  
			   
			        byte[] ba = output.toByteArray();  
			         excelFile = new ByteArrayInputStream(ba);  
			         output.flush();  
			         output.close();  
			         
		} catch (Exception e) {
			logger.info("######导出Excel失败######");
			addActionError(e.getMessage());
			return ERROR;
		}
		
		return SUCCESS; 
	}
	public InputStream getExcelFile() {
		return excelFile;
	}
	public void setExcelFile(InputStream excelFile) {
		this.excelFile = excelFile;
	}
	public String getExcelName() {
		String excelName="结算查询"+Constance.formateDate();
		 
			try {
				excelName = new String(excelName.getBytes(),"ISO8859-1");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				logger.info("生成excel名称失败了！");
			}
		 
		return  excelName;
		
	}
	public void setExcelName(String excelName) {
		this.excelName = excelName;
	}
	public SettingSearchCondition getSearchCondition() {
		return searchCondition;
	}
	public void setSearchCondition(SettingSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}
	
}	
